package com.techgene.kanban.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.techgene.kanban.dao.UserDao;
import com.techgene.kanban.domain.User;
import com.techgene.kanban.exception.KBDaoException;
import com.techgene.kanban.exception.KBServiceException;
import com.techgene.kanban.service.UserService;
import com.techgene.kanban.util.AppConstants;



@Service
public class UserServiceImpl implements UserService {

	private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private PlatformTransactionManager txManager;
	
	@Autowired
    UserDao userDao;
	
	
	
	
	/**
	 * Sends User information to DAO for storing.
	 * 
	 * @param UserProfile profile
	 * @throws KBServiceException
	 * @throws KBDaoException
	 * @return
	 */
	public String addUserData(User profile) throws KBServiceException{
		
		logger.debug(">> addUserData [profile = {} ]",profile);
		
		/*start of transaction*/
		TransactionDefinition def = new DefaultTransactionDefinition();
		TransactionStatus status = txManager.getTransaction(def);
		
		String result=null;
		
		try{
			
			/*
			 * calling storeUserData in AddUserDao implemented class to store the User information
			 * 
			 */
			String userName = profile.getUsername();
			String password = profile.getPassword();
			String firstName = profile.getFirstName();
			String lastName  = profile.getLastName();
			String role = profile.getRole();
			String email = profile.getEmail();
			 result = userDao.storeUserData(userName,password,firstName,lastName,email,role);
			 logger.debug("result={}",result);
			 
			 /* commit */
		 	 txManager.commit(status);
		 	 
			}catch (KBDaoException e) {
			
			   /* rollback */	
			   txManager.rollback(status);
			   throw new KBServiceException(e.getErrorCode(), e.getMessage(), e);
			   
	        }catch(Exception e)
	        { 
	        	 /* rollback */	
	    	  txManager.rollback(status);
	    	  throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_ADD_USER_DETAILS.code, AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_ADD_USER_DETAILS.msg);  
	    	  
	        }catch(Throwable e)
	        { 
	        	 /* rollback */
	    	  txManager.rollback(status); 
	    	  throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_ADD_USER_DETAILS.code, AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_ADD_USER_DETAILS.msg);  
	    	  
	        }
  		logger.debug("<< addUserData()");
		return result;
	}
	
	
	


	/**
	 * fetches user details
	 * 
	 * @param userName
	 * @throws KBServiceException
	 * @throws KBDaoException
	 * 
	 * @return userName
	 */

	public  User fetchUserDetails(String userName) throws KBServiceException, KBDaoException
	{
	
			logger.debug(" >> fetchUserDetails [userName = {} ]", userName);
		
		
			User profile ;
			TransactionDefinition def = new DefaultTransactionDefinition();
			TransactionStatus status = txManager.getTransaction(def);
			
		try {
	
			/*
			 * calling getUserDetails by using user name
			 */
			profile = userDao.fecthUserDetails(userName);
			logger.debug(" userProfile = {} ", profile);
			txManager.commit(status);
			
		}catch (KBDaoException e) {
			
			txManager.rollback(status);
			throw new KBServiceException(e.getErrorCode(), e.getMessage(), e);
	
		}catch (Exception e) {
			
			txManager.rollback(status);
			throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.code,AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.msg,e);
	
		}catch (Throwable e) {
			
			txManager.rollback(status);
			throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.code,AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.msg,e);
		}
		logger.debug(" << fetchUserDetails [ profile = {} ]",profile);
		return profile;
}

	
	
	
/**
 * updating user details
 * @param profile  User
 * @throws KBServiceException
 * @throws KBDaoException
 * 
 * @return result
 */

public String updateUserDetails(User profile) throws KBServiceException, KBDaoException{
	
	logger.debug(" >> updateUserDetails [ user = {} ]", profile);
	
	
	String result= null;
	TransactionDefinition def = new DefaultTransactionDefinition();
	TransactionStatus status = txManager.getTransaction(def);

	try {

		String userName = profile.getUsername();
		String firstName = profile.getFirstName();
		String lastName = profile.getLastName();
		String email = profile.getEmail();
		String role = profile.getRole();
		
		result = userDao.updateUserDetails(userName,firstName,lastName,email,role);
		logger.debug(" result = {} ", result);
		txManager.commit(status);
	}catch (KBDaoException e) {
		txManager.rollback(status);
		throw new KBServiceException(e.getErrorCode(), e.getMessage(), e);

	}catch (Exception e) {
		txManager.rollback(status);
		throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.code,AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.msg,e);

	}catch (Throwable e) {
		txManager.rollback(status);
		throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.code,AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.msg,e);

	}

	logger.debug(" << updateUserDeteils [ result ={} ]" , result);
	return result;
	}




/**
 * deleting user details
 * 
 * @param user 
 *              -pojo
 * @throws KBServiceException
 * @throws KBDaoException
 * 
 * @return result
 */

public String deleteUserDetails(String username) throws KBServiceException, KBDaoException{
	
		logger.debug(">>deleteUserDetails  [userName = {}]",username);
		
		String result = null;
		TransactionDefinition def = new DefaultTransactionDefinition();
		TransactionStatus status = txManager.getTransaction(def);
		
		try{
			
			//String userName = profile.getUsername();
			
			result = userDao.deleteUserDetails(username);
			logger.debug(" result = {} ", result);
			txManager.commit(status);
			
		}catch (KBDaoException e) {
			txManager.rollback(status);
			throw new KBServiceException(e.getErrorCode(), e.getMessage(), e);

		}catch (Exception e) {
			txManager.rollback(status);
			throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.code,AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.msg,e);

		}catch (Throwable e) {
			txManager.rollback(status);
			throw new KBServiceException(AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.code,AppConstants.ERROR.SERVICE_UNKNOWN_ERROR_UPDATE_DELETE_USER.msg,e);

		}
		
		logger.debug(" << deleteUserDeteils [ result ={} ]" , result);
		return result;
	}


	
	

}
